//  获取元素
const tbody = document.querySelector('tbody')
async function scoreList() {
	// 发请求
	const res = await axios({ url: 'score/list' })
	const data = res.data.data
	// 对数据进行拼接
	let html = ''
	// 遍历数组
	for (const key in data) {
		html +=`<tr>
        <th scope="row">${key}</th>
        <td>${data[key].name}</td>
        <td class="score" data-id='${key}' data-batch='${1}'>${data[key].score[0]}</td>
        <td class="score" data-id='${key}' data-batch='${2}'>${data[key].score[1]}</td>
        <td class="score" data-id='${key}' data-batch='${3}'>${data[key].score[2]}</td>
        <td class="score" data-id='${key}' data-batch='${4}'>${data[key].score[3]}</td>
        <td class="score" data-id='${key}' data-batch='${5}'>${data[key].score[4]}</td>
      </tr>`
	}
	tbody.innerHTML = html
}
scoreList() 
// 给tbody 绑定双击事件
tbody.addEventListener('dblclick', function (e) {
	// 使用
	if (e.target.classList.contains('score')) {
		console.log(111);
		const input = document.createElement('input')
		input.type = 'text'
		e.target.append(input)
		input.style.display = 'block'
		input.value = e.target.innerText
		input.focus()

		input.addEventListener('blur',async function () {
			const id = e.target.dataset.id
			const batch=e.target.dataset.batch
			
			
		 await axios({url: 'score/entry', method: 'post', data: {
					stu_id: id,
					batch:batch,
					score:input.value
			  }
		  })
			toastr.success('修改成功')
			scoreList() 
			
		})
		
	}
})